﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using TerminologiaSinacor.Presenter;
using TerminologiaSinacor.Presenter.Interface;

using TerminologiaSinacor.Utility; 

public partial class page_PesquisaFontesModulo : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            BindingModulo();
        }
    }

    private void BindingModulo()
    {
        Modulo modulo = new Modulo();
        //DrpModulo.DataTextField = "nome";
        //DrpModulo.DataValueField = "codigo";
        //DrpModulo.DataSource = modulo.search();
        //DrpModulo.DataBind();

        TerminologiaSinacor.Utility.BindingUtil.SetDataSource(modulo.search(), DrpModulo, "codigo", "NomeModuloNomePai", "", "[Modulo]");
        TerminologiaSinacor.Utility.BindingUtil.SetDataSource(null, DrpFonte, "IdFonteIdFonteTipoNomeFonte", "FonteTipo", "", "[Fonte]");
        TerminologiaSinacor.Utility.BindingUtil.SetDataSource(null, DrpFiltro, "nome", "nome", "", "[Filtro]");

        ////
        //Fontes fontes = new Fontes();
        //DrpFonte.DataTextField = "FonteTipo";
        //DrpFonte.DataValueField = "Fonte";
        //DrpFonte.DataSource = fontes.search();
        //DrpFonte.DataBind();
        ////
        //Filtro filtro = new Filtro();
        //DrpFiltro.DataTextField = "nome";
        //DrpFiltro.DataValueField = "codigo";
        //DrpFiltro.DataSource = filtro.search(null);
        //DrpFiltro.DataBind();
    }

    private void BindingFonte()
    {
        TxtFonte.Text = "";
        if ( DrpModulo.SelectedValue !="")
        {
            Fontes fontes = new Fontes();   
            fontes.Modulo = DrpModulo.SelectedValue;

            //DrpFonte.DataValueField = "IdFonteIdFonteTipoNomeFonte";
            //DrpFonte.DataTextField = "Fonte";
            //DrpFonte.DataSource = fontes.search(fontes.Modulo);
            //DrpFonte.DataBind();
            TerminologiaSinacor.Utility.BindingUtil.SetDataSource(fontes.search(fontes.Modulo), DrpFonte, "IdFonteIdFonteTipoNomeFonte", "FonteTipo", "", "[Fonte]");
  
        }
    }

    private void BindingFiltro()
    {
        if ((DrpModulo.SelectedValue != "") && (DrpFonte.SelectedValue !=""))
        {
            string[] prmFonte = DrpFonte.SelectedValue.Split('#');

            string IdFonte = prmFonte[0].ToString();
            string IdTipoFonte = prmFonte[1].ToString();
            //TxtFonte.Text = prmFonte[2].ToString();   
            
            Filtro filtro = new Filtro();
            //DrpFiltro.DataTextField = "nome";
            //DrpFiltro.DataValueField = "nome";
            //DrpFiltro.DataSource = filtro.search(DrpModulo.SelectedValue, IdTipoFonte, IdFonte);
            //DrpFiltro.DataBind();

            TerminologiaSinacor.Utility.BindingUtil.SetDataSource(filtro.search(DrpModulo.SelectedValue, IdTipoFonte, IdFonte), DrpFiltro, "nome", "nome", "", "[Filtro]");


        }
    }

    protected void DrpFonte_SelectedIndexChanged(object sender, EventArgs e)
    {
        if ((this.DrpFonte.SelectedValue != "") && (this.DrpFonte.SelectedValue != null))
        {
            string[] prm = this.DrpFonte.SelectedValue.Split('#');
            TxtFonte.Text = prm[2];
            BindingFiltro();
        }
        else
        {
            iniciaDrpFiltro();
        }
    }

    protected void DrpModulo_SelectedIndexChanged(object sender, EventArgs e)
    {
        if ((this.DrpModulo.SelectedValue != null) && (this.DrpModulo.SelectedValue != ""))
        {
            BindingFonte();
        }
        else
        {
            iniciaDrpFonte();
            iniciaDrpFiltro();
        }
    }

    private void iniciaDrpFonte()
    {
        TerminologiaSinacor.Utility.BindingUtil.SetDataSource(null, DrpFonte, "IdFonteIdFonteTipoNomeFonte", "FonteTipo", "", "[Fonte]");
    }

    private void iniciaDrpFiltro()
    {
        TerminologiaSinacor.Utility.BindingUtil.SetDataSource(null, DrpFiltro, "nome", "nome", "", "[Filtro]");
    }

    protected void btnPesquisar_Click(object sender, EventArgs e)
    {
        if ((DrpModulo.SelectedValue != null) &&  (this.DrpFonte.SelectedValue != null) && (DrpFiltro.SelectedValue != null))
        {
            string TipoFonteId = "";
            if (this.DrpFonte.SelectedValue != "")
            {
                string[] prm = this.DrpFonte.SelectedValue.Split('#');
                if ( prm.Length > 0) TipoFonteId = prm[1];
            }
                string url = "~/page/FontesModulo.aspx?ModuloId=" + DrpModulo.SelectedValue + "&TipoFonteId=" + TipoFonteId + "&Filtro=" + DrpFiltro.SelectedValue;

                url += "&lblModuloText=" + this.DrpModulo.SelectedItem.Text;
                //url +="&LblModuloPaiText=" + TxtModuloPai.Text;
                url += "&lblFonteText=" + this.DrpFonte.SelectedItem.Text;
                url += "&lblFonteNomeText=" + this.TxtFonte.Text;
                url += "&LblFiltroText=" + this.DrpFiltro.SelectedItem.Text;

                this.Page.Response.Redirect(url);
          
        }
    }

    protected void btnVoltar_Click(object sender, EventArgs e)
    {
        this.Page.Response.Redirect("~/Default.aspx");
    }
}
